package com.dule.Medic.pages;

import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;  
import org.apache.tapestry5.annotations.CleanupRender; 

import com.dule.Medic.beans.DoctorBean;
import com.dule.Medic.model.Doctor;
import com.dule.Medic.service.DoctorService;

public class DrRegister 
{
	@Persist("flash")  
	@Property     
	private Doctor doctor;    
	
	@SuppressWarnings("unused")
	@Property
	@Persist
	private String message; 
	
	@Inject        
	private DoctorService doctorService;   
	@SetupRender   
	public void setUp()
	{        
		doctor = new DoctorBean();       
	}            
	@OnEvent(component="doctorform")  
	public Object register()
	{    
		message =null;
		Doctor registeredDoctor = doctorService.retrive(doctor);    
		if(registeredDoctor == null)
		{           
			Long id = doctorService.save(doctor);       
			System.out.println(doctorService.get(id).getUserName());                         
			message = doctor.getUserName()+" has been successfully registered"; 
			return FirstPage.class;
		}     
		message = doctor.getUserName()+" allready exists!!!";
		return this;         
	} 
	
	@CleanupRender     
	public void cleanUp()
	{             
		message =null;        
	}  
}
